<?php

namespace App\Admin\Controllers;

use App\Models\Device;
use App\Models\PhoneNumber;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class PhoneNumberController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '号码';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new PhoneNumber());

        $grid->column('id', __('Id'));
        $grid->column('number', __('Number'));
        $grid->column('imei', __('Imei'));
        $grid->column('device_Id', __('Device Id'));
        $grid->column('operator', __('Operator'));
        $grid->column('sms_at', __('Sms at'));
        $grid->column('count_sms', __('短信总计'))->display(function () {
            return "<a href='" . url('admin/sms-logs') . "?" . http_build_query(['phone_number' => $this->number]) . "'>$this->count_sms</a>";
        });
        $grid->column('created_at', __('Created at'))->sortable();;
        $grid->column('updated_at', __('Updated at'))->sortable();;
        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            $filter->equal('number', __('Number'));
            $filter->equal('imei', __('Imei'));
//            $filter->equal('imsi', __('Imsi'));
            $filter->equal('device_id', __('Device id'))->select(Device::all()->pluck('full_tag', 'device_id'));

            $filter->equal('operator', __('Operator'))->select(PhoneNumber::OPERATORS);

        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(PhoneNumber::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('number', __('Number'));
        $show->field('imei', __('Imei'));
        $show->field('device_Id', __('Device Id'));
        $show->field('operator', __('Operator'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new PhoneNumber());

        $form->select('device_id', __('Device id'))->options(Device::all()->pluck('full_tag', 'device_id'))->required();
        $form->mobile('number', __('Number'));
        $form->text('imei', __('Imei'))->required();
        $form->select('operator', __('Operator'))->options(PhoneNumber::OPERATORS)->default(PhoneNumber::CMCC);

        return $form;
    }
}
